<template>
  <page-view :title="false" >
    <a-card :bordered="false">

      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="品牌名">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.enable" placeholder="请选择" default-value="0">
                  <a-select-option value="true">启用</a-select-option>
                  <a-select-option value="false">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons" :style="{ float: 'right', overflow: 'hidden' }">
                <a-button type="primary" @click="loadTableData()">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {pageNo: 1, pageSize: 10}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" v-action:productBrandAdd @click="handleCreate()">新增</a-button>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :rowKey="record => record.id"
        :dataSource="brandList"
        :pagination="pagination"
        :scroll="{ x: 1800 }"
        @change="handleTableChange">

        <span slot="enable" slot-scope="text">
          {{ text | enableFilter }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a style="margin-right:10px;" href="javascript:;" v-action:productBrandEdit @click="handleEdit(record)">编辑</a>
          <a href="javascript:;" v-action:productBrandDelete @click="handleDel(record)">删除</a>
        </span>
        <span slot="image" slot-scope="text">
          <img key="brandImage" :src="text">
        </span>

      </a-table>

      <brand-modal ref="brandModal" @ok="handleSaveOk" />

    </a-card>
  </page-view>

</template>
<script>
import { PageView } from '@/layouts'
import BrandModal from './BrandModal'
import { brandPage, brandDelete } from '@/api/product'

export default {
  name: 'Brand',
  components: {
    PageView,
    BrandModal
  },
  data () {
    return {
      columns,
      pagination: {},
      brandList: [],
      queryParam: { pageNo: 1, pageSize: 10 }
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      console.log(pagination)
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      this.queryParam = {
        pageSize: pagination.pageSize,
        pageNo: pagination.current,
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters
      }
      this.loadTableData()
    },
    handleEdit (record) {
      this.$refs.brandModal.handleEdit(record)
    },
    handleCreate () {
      this.$refs.brandModal.handleCreate()
    },
    handleDel (record) {
      brandDelete(record).then(res => {
        if (res.status === 0) {
          this.loadTableData()
          this.$message.success('成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // handler
    handleSaveOk () {
      console.log('接受子組件的通知')
      this.loadTableData()
    },
    loadTableData () {
      var _this = this
      brandPage(this.queryParam).then(res => {
        const result = res.data
        _this.brandList = result.records
        _this.pagination = {
          total: result.total,
          pageSize: result.size,
          current: result.current
        }
        console.log(_this.pagination)
      })
    }
  },
  created () {
    this.loadTableData()
  },
  filters: {
    enableFilter (enable) {
      const enableMap = {
        true: '启用',
        false: '禁用'
      }
      return enableMap[enable]
    }
  }
}

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id',
    width: '12%'
  },
  {
    title: '品牌名',
    dataIndex: 'name',
    key: 'name',
    width: '12%'
  },
  {
    title: '图片',
    dataIndex: 'image',
    key: 'image',
    scopedSlots: { customRender: 'image' }
  },

  {
    title: 'enable',
    dataIndex: 'enable',
    key: 'enable',
    scopedSlots: { customRender: 'enable' }
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime'
  },
  {
    title: '创建者',
    dataIndex: 'creater',
    key: 'creater',
    width: '10%'
  },
  {
    title: '更新者',
    dataIndex: 'updater',
    key: 'updater',
    width: '10%'
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
    width: '20%'
  }
]
</script>
